package com.jy.api.controller.clerk;

import com.jy.api.dao.EmployeeDao;
import com.jy.api.domain.Employee;
import com.jy.api.service.SaleService;
import com.jy.api.vo.clerk.SaleInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * Created by lihu on 2018/3/24.
 */
@Slf4j
@RestController
@RequestMapping(value = "v1/clerk", produces = "application/json;charset=utf-8")
public class ClerkSaleController {

    @Autowired
    EmployeeDao employeeDao;

    @Autowired
    SaleService saleService;


    @RequestMapping(method = RequestMethod.GET, value = "/sale-info")
    public ResponseEntity coupon(@RequestParam(value = "storeId") String storeId, Principal principal) {
        try {
            Employee employee = employeeDao.findByStoreIdAndUsernameAndEnabled(storeId,principal.getName(), true);
            if (employee == null) {
                return new ResponseEntity("店员信息不存在", HttpStatus.BAD_REQUEST);
            }
            SaleInfoVo saleInfoVo = saleService.querySaleInfo(storeId,
                    principal.getName());
            saleInfoVo.setTarget(employee.getTarget());
            saleInfoVo.accuracy();
            return new ResponseEntity(saleInfoVo, HttpStatus.OK);
        } catch (Exception ex) {
            log.error("checkCounter is error==> storeId:{}", storeId, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }
}
